const fs = require('fs')
const path = require('path')
const { lookDir } = require('../build/look-dir')

const ROOT_PATH = path.resolve(__dirname, '..')
const PAGE_PATH = path.join(ROOT_PATH, '/src')

exports.getHtml = function() {
  let htmlArray = []
  lookDir(PAGE_PATH, null, null, (fullPath, fileInfo, file) => {
    // console.log('fullPath=', fullPath)
    if (fileInfo.isDirectory()) {
      lookDir(fullPath, null, null, (fullPath2, fileInfo2, file2) => {
        if (fileInfo2.isFile()) {
          if (file2 === '.name') {
            const htmlName = readDirHtmlName(fullPath2)
            const html = {
              path: './' + file + '/index.html',
              name: htmlName
            }
            htmlArray.push(html)
          }
        } else if (fileInfo2.isDirectory()) {
          lookDir(fullPath2, null, null, (fullPath3, fileInfo3, file3) => {
            if (fileInfo3.isFile()) {
              if (file3 === '.name') {
                const htmlName = readDirHtmlName(fullPath3)
                const html = {
                  path: './' + file + '/' + file2 + '/index.html',
                  name: htmlName
                }
                htmlArray.push(html)
              }
            }
          })
        }
      })
    }
  })
  console.log('htmlArray=', htmlArray)
  return htmlArray
}

function readDirHtmlName(htmlNamePath) {
  // console.log('文件名 htmlNamePath=', htmlNamePath)
  if (!htmlNamePath) return ''
  let name = fs.readFileSync(htmlNamePath, 'utf-8');
  name = trimStr(name)
  return name
}

function trimStr(str) {
  if (!str) return ''
  return str.replace(/(^\s*)|(\s*$)/g,"");
}